package com.chen.controller.admin;

import com.alibaba.fastjson.JSON;
import com.chen.pojo.Dept;
import com.chen.service.dept.DeptService;
import com.chen.util.DataGridViewResult;
import com.chen.util.SystemConstrant;
import com.chen.vo.DeptVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/dept")
public class DeptController {

    @Resource
    private DeptService deptService;

    /**
     * 查询部门列表
     * @param deptVo
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(DeptVo deptVo){
        //设置分页信息
        PageHelper.startPage(deptVo.getPage(),deptVo.getLimit());
        //调用分页查询方法
        List<Dept> deptList = deptService.queryAllDept(deptVo);
        //创建分页对象
        PageInfo<Dept> pageInfo = new PageInfo<Dept>(deptList);
        //返回数据
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 添加部门
     * @param dept
     * @return
     */
    @RequestMapping(value = "/addDept",produces = {"application/json;charset=UTF-8"})
    public String addDept(Dept dept){
        HashMap<String, Object> map = new HashMap<String,Object>();
        if (deptService.addDept(dept)>0){
            map.put(SystemConstrant.SUCCESS,true);//成功
            map.put(SystemConstrant.MESSAGE,"添加成功");
        }else{
            map.put(SystemConstrant.SUCCESS,false);//失败
            map.put(SystemConstrant.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 修改部门
     * @param dept
     * @return
     */
    @RequestMapping(value = "/updateDept",produces = {"application/json;charset=UTF-8"})
    public String updateDept(Dept dept){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用修改部门的方法
        if(deptService.updateDept(dept)>0){
            map.put(SystemConstrant.SUCCESS,true);//成功
            map.put(SystemConstrant.MESSAGE,"修改成功");
        }else{
            map.put(SystemConstrant.SUCCESS,false);//失败
            map.put(SystemConstrant.MESSAGE,"修改失败");
        }
        //将map集合以JSON格式返回
        return JSON.toJSONString(map);
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteById",produces = {"application/json;charset=UTF-8"})
    public String deleteById(Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用删除部门的方法
        if(deptService.deleteById(id)>0){
            map.put(SystemConstrant.SUCCESS,true);//成功
            map.put(SystemConstrant.MESSAGE,"删除成功");
        }else{
            map.put(SystemConstrant.SUCCESS,false);//失败
            map.put(SystemConstrant.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 查询所有部门并且显示在员工管理界面
     * @return
     */
    @RequestMapping(value = "/queryDept",produces = {"application/json;charset=UTF-8"})
    public String queryDept(){
        return JSON.toJSONString(deptService.queryDept());
    }
}
